package com.app.micaihu.view.main.fragment;

import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.app.config.LoadViewConfig;
import com.app.micaihu.R;
import com.app.micaihu.adapter.VideoAdapter1;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListFragment;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.news.VideoTranslate;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.view.main.MainActivity;
import com.app.utils.pulltorefresh.PullToRefreshBase;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by linkai on 2017/5/24.
 */
public class VideoFragment2 extends BaseListFragment<VideoTranslate> {


    private int mark;

    //操作监听
    private MainActivity.OnRefreshingListener mOnRefreshingListener;

    @Override
    protected void changeView() {
        mListView.setOnLastItemVisibleListener(null);
    }


    @Override
    protected void changeData(final boolean isRefresh) {
        Map<String, String> parms = new HashMap<>();
        parms.put("mark", mark + "");

        HttpListener<DataBean<List<VideoTranslate>>> httpListener = new HttpListener<DataBean<List<VideoTranslate>>>() {
            @Override
            public void onStart() {
                //updateLoadView(LOADING, null);
                setLoadView(LOADING, null);
                if (isRefresh && mark == 1 && mOnRefreshingListener != null) {
                    //说明是手指下拉刷新
                    mOnRefreshingListener.onDataRefreshingStart();
                }
            }

            @Override
            public void onError(VolleyError error) {
                //updateLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
                if (isRefresh && mark == 1 && mOnRefreshingListener != null) {
                    //说明是手指下拉刷新
                    mOnRefreshingListener.onDataRefreshingEnd();
                }
            }

            @Override
            public void onSuccess(DataBean<List<VideoTranslate>> response) {

                if (response.noErrorData()) {
                    List<VideoTranslate> videolist = response.getData();
                    if (videolist != null && videolist.size() > 0) {
                        if (dataList == null) {
                            dataList = new ArrayList<>();
                        }
                        dataList.clear();
                        dataList.addAll(videolist);

                        if (mAdapter == null) {
                            mAdapter = new VideoAdapter1(dataList, activity);
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, "");
                    } else {
                        setLoadView(DATAERROR, getString(R.string.pullup_no_data));
                    }
                } else {
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };
        addPostCacheRequest(URIContant.VIDEO_ROOM_LIST, new TypeToken<DataBean<List<VideoTranslate>>>() {
        }.getType(), parms, httpListener);
    }

    /**
     * LoadViewd 方法
     *
     * @param code 传入对应的值
     * @param msg  提示的的文字
     */
    protected void setLoadView(int code, CharSequence msg) {

        switch (code) {
            case NO_NET://网络错误
                if (dataList == null || dataList.size() == 0) {
                    mLoadView.showErrorPage(msg);
                }
                if (mListView != null) {
                    mListView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mListView.onRefreshComplete();
                            //更改 底部导航栏
                            if (mOnRefreshingListener != null) {
                                mOnRefreshingListener.onDataRefreshingEnd();
                            }
                        }
                    }, 800);
                }
                break;
            case DATAERROR://数据错误
                if (dataList == null || dataList.size() == 0) {
                    mLoadView.showErrorPage(msg);
                }
                if (mListView != null) {
                    mListView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mListView.onRefreshComplete();
                            //更改 底部导航栏
                            if (mOnRefreshingListener != null) {
                                mOnRefreshingListener.onDataRefreshingEnd();
                            }
                        }
                    }, 800);
                }
                break;
            case LOADING://加载中
                if (dataList == null || dataList.size() == 0) {
                    mLoadView.showLoadPage(msg);
                }
                break;
            case LOADSUCES://加载成功
                if (dataList != null && dataList.size() > 0) {
                    mLoadView.showSuccess();
                } else {
                    mLoadView.showEmptyPage(LoadViewConfig.EMPTY_DEFAULT, "");
                }
                if (mListView != null) {
                    mListView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mListView.onRefreshComplete();
                            //更改 底部导航栏
                            if (mOnRefreshingListener != null) {
                                mOnRefreshingListener.onDataRefreshingEnd();
                            }
                        }
                    }, 800);
                }
                break;
        }
        if (msg != null && !"".equals(msg)) {
            notify_view_text.setText(msg);
            notify_view_text.setVisibility(View.VISIBLE);
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    notify_view_text.setVisibility(View.GONE);
                }
            }, 2000);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mark = 1;
        dataPage = 1;//数据页数
        changeData(true);
        StatService.onEvent(activity, "066", "视频列表下拉", 1);//统计事件的点击次数
    }


    public void setOnRefreshingListener(MainActivity.OnRefreshingListener mOnRefreshingListener) {

        this.mOnRefreshingListener = mOnRefreshingListener;
    }
}
